﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLiNhaSachBUS;
namespace QuanLiNhaSachGUI
{
    public partial class frmThongKe : Form
    {
        public frmThongKe()
        {
            InitializeComponent();
            
        }

        private void OK_Click(object sender, EventArgs e)
        {

            if (rbBan.Checked && rbTuaDe.Checked)
            {
                dataThongKe.DataSource = ThongKeBUS.thongKeBanSachTheoTuaDe(dateTimePicker1.Value, dateTimePicker2.Value);
            }
            if (rbNhap.Checked && rbTuaDe.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeNhapSachTheoTuaDe(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbBan.Checked && rbTheLoai.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeBanSachTheoTheLoai(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbNhap.Checked && rbTheLoai.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeNhapSachTheoTheLoai(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbBan.Checked && rbNhanVien.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeBanSachTheoNhanVien(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbNhap.Checked && rbNhanVien.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeNhapSachTheoNhanVien(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbE.Checked && rbTuaDe.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeSachKhongBanDuocTheoTuaDe(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbE.Checked && rbTheLoai.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeSachKhongBanDuocTheoTheLoai(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbBan.Checked && rbNXB.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeBanSachTheoNXB(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbNhap.Checked && rbNXB.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeNhapSachTheoNXB(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbE.Checked && rbNXB.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeSachKhongBanDuocTheoNXB(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbBan.Checked && rbTacGia.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeBanSachTheoTacGia(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbNhap.Checked && rbTacGia.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeNhapSachTheoTacGia(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbE.Checked && rbTacGia.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeSachKhongBanDuocTheoTacGia(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbBan.Checked && rbNhaCungCap.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeBanSachTheoNhaCungCap(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbNhap.Checked && rbNhaCungCap.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeNhapSachTheoNhaCungCap(dateTimePicker1.Value, dateTimePicker2.Value);
            if (rbE.Checked && rbNhaCungCap.Checked)
                dataThongKe.DataSource = ThongKeBUS.thongKeSachKhongBanDuocTheoNhaCungCap(dateTimePicker1.Value, dateTimePicker2.Value);
            dataThongKe.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            TinhTien();
        }

        public string splitTien(long s)
        {
            string st = s.ToString();
            int len = st.Length;
            for (int i = len - 2; i > 0; i--)
            {
                if ((len - i) % 3 == 0)
                    st = st.Insert(i, ",");
            }
            return st;
        }

        public void TinhTien()
        {
            long tongtien = 0, min = long.MaxValue, max = 0, t;
            int OT = 3;
            if (rbTuaDe.Checked == true)
                OT = 5;
            for (int i = 0; i < dataThongKe.RowCount; i++)
            {
                t = long.Parse(dataThongKe.Rows[i].Cells[OT].Value.ToString());
                tongtien +=  t;
                if (max < t) max = t;
                if (min > t) min = t;
            }
            if (min == long.MaxValue)
                min = 0;
            tbLon.Text = splitTien(max);
            tbNho.Text = splitTien(min);
            tbTong.Text = splitTien(tongtien);
        }

        private void frmThongKe_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmMain.listForm.Remove(this);
        }

        private void Report_Click(object sender, EventArgs e)
        {
            if (rbBan.Checked && rbTuaDe.Checked)
            {
                new frmReportViewer().viewTuaBan(ThongKeBUS.thongKeBanSachTheoTuaDe(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbNhap.Checked && rbTuaDe.Checked)
            {
                new frmReportViewer().viewTuaNhap(ThongKeBUS.thongKeNhapSachTheoTuaDe(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbE.Checked && rbTuaDe.Checked)
            {
                new frmReportViewer().viewTuaE(ThongKeBUS.thongKeSachKhongBanDuocTheoTuaDe(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbBan.Checked && rbTheLoai.Checked)
            {
                new frmReportViewer().viewTLBan(ThongKeBUS.thongKeBanSachTheoTheLoai(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbNhap.Checked && rbTheLoai.Checked)
            {
                new frmReportViewer().viewTLNhap(ThongKeBUS.thongKeNhapSachTheoTheLoai(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbE.Checked && rbTheLoai.Checked)
            {
                new frmReportViewer().viewTLE(ThongKeBUS.thongKeSachKhongBanDuocTheoTheLoai(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbBan.Checked && rbTacGia.Checked)
            {
                new frmReportViewer().viewTGBan(ThongKeBUS.thongKeBanSachTheoTacGia(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbNhap.Checked && rbTacGia.Checked)
            {
                new frmReportViewer().viewTGBan(ThongKeBUS.thongKeNhapSachTheoTacGia(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbE.Checked && rbTacGia.Checked)
            {
                new frmReportViewer().viewTGE(ThongKeBUS.thongKeSachKhongBanDuocTheoTacGia(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbBan.Checked && rbNXB.Checked)
            {
                new frmReportViewer().viewNXBBan(ThongKeBUS.thongKeBanSachTheoNXB(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbNhap.Checked && rbNXB.Checked)
            {
                new frmReportViewer().viewNXBBan(ThongKeBUS.thongKeNhapSachTheoNXB(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbE.Checked && rbNXB.Checked)
            {
                new frmReportViewer().viewNXBE(ThongKeBUS.thongKeSachKhongBanDuocTheoNXB(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbBan.Checked && rbNhaCungCap.Checked)
            {
                new frmReportViewer().viewNCCBan(ThongKeBUS.thongKeBanSachTheoNhaCungCap(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbNhap.Checked && rbNhaCungCap.Checked)
            {
                new frmReportViewer().viewNCCBan(ThongKeBUS.thongKeNhapSachTheoNhaCungCap(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbE.Checked && rbNhaCungCap.Checked)
            {
                new frmReportViewer().viewNCCE(ThongKeBUS.thongKeSachKhongBanDuocTheoNhaCungCap(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbBan.Checked && rbNhanVien.Checked)
            {
                new frmReportViewer().viewNVBan(ThongKeBUS.thongKeBanSachTheoNhanVien(dateTimePicker1.Value, dateTimePicker2.Value));
            }
            if (rbNhap.Checked && rbNhanVien.Checked)
            {
                new frmReportViewer().viewNVBan(ThongKeBUS.thongKeNhapSachTheoNhanVien(dateTimePicker1.Value, dateTimePicker2.Value));
            }
        }
    }
}
